{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "initial_id",
   "metadata": {
    "collapsed": true,
    "ExecuteTime": {
     "end_time": "2023-11-23T11:13:35.488758800Z",
     "start_time": "2023-11-23T11:13:34.894595400Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": "       movie_name  movie_box_split_unit movie_box_rate movie_avg_seat  \\\n0           拯救嫌疑人                904.27          39.4%           1.7%   \n1            无价之宝                496.16          21.6%           1.7%   \n2     饥饿游戏：鸣鸟与蛇之歌                319.74          13.9%           1.3%   \n3              红猪                116.75           5.0%           1.9%   \n4        志愿军：雄兵出击                 69.92           3.0%           2.4%   \n...           ...                   ...            ...            ...   \n2974         王牌二哈                  0.00          <0.1%             --   \n2975           险诈                  0.00          <0.1%             --   \n2976        踢球吧少年                  0.00          <0.1%             --   \n2977         猫狗武林                  0.00          <0.1%             --   \n2978    龙珠超：超级人造人                  0.00          <0.1%             --   \n\n     movie_avg_show  movie_show_count movie_show_count_rate    cur_date  \n0               2.6             82864                 28.5%  2023-11-21  \n1               2.2             54513                 18.7%  2023-11-21  \n2               2.0             43835                 15.1%  2023-11-21  \n3               2.3             14467                  4.9%  2023-11-21  \n4               2.7              6708                  2.3%  2023-11-21  \n...             ...               ...                   ...         ...  \n2974            0.0                 1                 <0.1%  2023-11-01  \n2975            0.0                 1                 <0.1%  2023-11-01  \n2976            0.0                 1                 <0.1%  2023-11-01  \n2977            0.0                 5                 <0.1%  2023-11-01  \n2978            0.0                 2                 <0.1%  2023-11-01  \n\n[2979 rows x 8 columns]",
      "text/html": "<div>\n<style scoped>\n    .dataframe tbody tr th:only-of-type {\n        vertical-align: middle;\n    }\n\n    .dataframe tbody tr th {\n        vertical-align: top;\n    }\n\n    .dataframe thead th {\n        text-align: right;\n    }\n</style>\n<table border=\"1\" class=\"dataframe\">\n  <thead>\n    <tr style=\"text-align: right;\">\n      <th></th>\n      <th>movie_name</th>\n      <th>movie_box_split_unit</th>\n      <th>movie_box_rate</th>\n      <th>movie_avg_seat</th>\n      <th>movie_avg_show</th>\n      <th>movie_show_count</th>\n      <th>movie_show_count_rate</th>\n      <th>cur_date</th>\n    </tr>\n  </thead>\n  <tbody>\n    <tr>\n      <th>0</th>\n      <td>拯救嫌疑人</td>\n      <td>904.27</td>\n      <td>39.4%</td>\n      <td>1.7%</td>\n      <td>2.6</td>\n      <td>82864</td>\n      <td>28.5%</td>\n      <td>2023-11-21</td>\n    </tr>\n    <tr>\n      <th>1</th>\n      <td>无价之宝</td>\n      <td>496.16</td>\n      <td>21.6%</td>\n      <td>1.7%</td>\n      <td>2.2</td>\n      <td>54513</td>\n      <td>18.7%</td>\n      <td>2023-11-21</td>\n    </tr>\n    <tr>\n      <th>2</th>\n      <td>饥饿游戏：鸣鸟与蛇之歌</td>\n      <td>319.74</td>\n      <td>13.9%</td>\n      <td>1.3%</td>\n      <td>2.0</td>\n      <td>43835</td>\n      <td>15.1%</td>\n      <td>2023-11-21</td>\n    </tr>\n    <tr>\n      <th>3</th>\n      <td>红猪</td>\n      <td>116.75</td>\n      <td>5.0%</td>\n      <td>1.9%</td>\n      <td>2.3</td>\n      <td>14467</td>\n      <td>4.9%</td>\n      <td>2023-11-21</td>\n    </tr>\n    <tr>\n      <th>4</th>\n      <td>志愿军：雄兵出击</td>\n      <td>69.92</td>\n      <td>3.0%</td>\n      <td>2.4%</td>\n      <td>2.7</td>\n      <td>6708</td>\n      <td>2.3%</td>\n      <td>2023-11-21</td>\n    </tr>\n    <tr>\n      <th>...</th>\n      <td>...</td>\n      <td>...</td>\n      <td>...</td>\n      <td>...</td>\n      <td>...</td>\n      <td>...</td>\n      <td>...</td>\n      <td>...</td>\n    </tr>\n    <tr>\n      <th>2974</th>\n      <td>王牌二哈</td>\n      <td>0.00</td>\n      <td>&lt;0.1%</td>\n      <td>--</td>\n      <td>0.0</td>\n      <td>1</td>\n      <td>&lt;0.1%</td>\n      <td>2023-11-01</td>\n    </tr>\n    <tr>\n      <th>2975</th>\n      <td>险诈</td>\n      <td>0.00</td>\n      <td>&lt;0.1%</td>\n      <td>--</td>\n      <td>0.0</td>\n      <td>1</td>\n      <td>&lt;0.1%</td>\n      <td>2023-11-01</td>\n    </tr>\n    <tr>\n      <th>2976</th>\n      <td>踢球吧少年</td>\n      <td>0.00</td>\n      <td>&lt;0.1%</td>\n      <td>--</td>\n      <td>0.0</td>\n      <td>1</td>\n      <td>&lt;0.1%</td>\n      <td>2023-11-01</td>\n    </tr>\n    <tr>\n      <th>2977</th>\n      <td>猫狗武林</td>\n      <td>0.00</td>\n      <td>&lt;0.1%</td>\n      <td>--</td>\n      <td>0.0</td>\n      <td>5</td>\n      <td>&lt;0.1%</td>\n      <td>2023-11-01</td>\n    </tr>\n    <tr>\n      <th>2978</th>\n      <td>龙珠超：超级人造人</td>\n      <td>0.00</td>\n      <td>&lt;0.1%</td>\n      <td>--</td>\n      <td>0.0</td>\n      <td>2</td>\n      <td>&lt;0.1%</td>\n      <td>2023-11-01</td>\n    </tr>\n  </tbody>\n</table>\n<p>2979 rows × 8 columns</p>\n</div>"
     },
     "execution_count": 2,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "import pandas as pd\n",
    "import numpy as np\n",
    "\n",
    "df = pd.read_csv('../static/data/movie_box_rate.csv')\n",
    "df"
   ]
  },
  {
   "cell_type": "markdown",
   "source": [],
   "metadata": {
    "collapsed": false
   },
   "id": "36b13183313ccae6"
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "outputs": [
    {
     "data": {
      "text/plain": "{'name': ['拯救嫌疑人',\n  '无价之宝',\n  '惊奇队长2',\n  '河边的错误',\n  '二手杰作',\n  '饥饿游戏：鸣鸟与蛇之歌',\n  '志愿军：雄兵出击',\n  '前任4：英年早婚',\n  '坚如磐石',\n  '追缉'],\n 'data': ['44179.18万',\n  '12154.24万',\n  '10808.06万',\n  '6553.7万',\n  '6264.47万',\n  '4002.9万',\n  '3850.42万',\n  '2840.56万',\n  '2719.23万',\n  '2044.9199999999998万']}"
     },
     "execution_count": 10,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df_box_top10 = df[['movie_name','movie_box_split_unit']].groupby('movie_name').sum().sort_values('movie_box_split_unit', ascending=False).reset_index().head(10)"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2023-11-23T07:29:14.026602100Z",
     "start_time": "2023-11-23T07:29:14.013832800Z"
    }
   },
   "id": "deb2d96cac1806e3"
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "outputs": [
    {
     "data": {
      "text/plain": "      cur_date  movie_box_split_unit\n0   2023-11-01               3856.02\n1   2023-11-02               3693.65\n2   2023-11-03               5900.90\n3   2023-11-04              11225.05\n4   2023-11-05               8143.11\n5   2023-11-06               2914.62\n6   2023-11-07               2786.77\n7   2023-11-08               2675.15\n8   2023-11-09               2763.43\n9   2023-11-10               6543.24\n10  2023-11-11              11994.53\n11  2023-11-12               8402.88\n12  2023-11-13               2765.51\n13  2023-11-14               2608.06\n14  2023-11-15               2565.58\n15  2023-11-16               2589.46\n16  2023-11-17               4645.31\n17  2023-11-18               9727.49\n18  2023-11-19               7110.86\n19  2023-11-20               2341.72\n20  2023-11-21               2288.61",
      "text/html": "<div>\n<style scoped>\n    .dataframe tbody tr th:only-of-type {\n        vertical-align: middle;\n    }\n\n    .dataframe tbody tr th {\n        vertical-align: top;\n    }\n\n    .dataframe thead th {\n        text-align: right;\n    }\n</style>\n<table border=\"1\" class=\"dataframe\">\n  <thead>\n    <tr style=\"text-align: right;\">\n      <th></th>\n      <th>cur_date</th>\n      <th>movie_box_split_unit</th>\n    </tr>\n  </thead>\n  <tbody>\n    <tr>\n      <th>0</th>\n      <td>2023-11-01</td>\n      <td>3856.02</td>\n    </tr>\n    <tr>\n      <th>1</th>\n      <td>2023-11-02</td>\n      <td>3693.65</td>\n    </tr>\n    <tr>\n      <th>2</th>\n      <td>2023-11-03</td>\n      <td>5900.90</td>\n    </tr>\n    <tr>\n      <th>3</th>\n      <td>2023-11-04</td>\n      <td>11225.05</td>\n    </tr>\n    <tr>\n      <th>4</th>\n      <td>2023-11-05</td>\n      <td>8143.11</td>\n    </tr>\n    <tr>\n      <th>5</th>\n      <td>2023-11-06</td>\n      <td>2914.62</td>\n    </tr>\n    <tr>\n      <th>6</th>\n      <td>2023-11-07</td>\n      <td>2786.77</td>\n    </tr>\n    <tr>\n      <th>7</th>\n      <td>2023-11-08</td>\n      <td>2675.15</td>\n    </tr>\n    <tr>\n      <th>8</th>\n      <td>2023-11-09</td>\n      <td>2763.43</td>\n    </tr>\n    <tr>\n      <th>9</th>\n      <td>2023-11-10</td>\n      <td>6543.24</td>\n    </tr>\n    <tr>\n      <th>10</th>\n      <td>2023-11-11</td>\n      <td>11994.53</td>\n    </tr>\n    <tr>\n      <th>11</th>\n      <td>2023-11-12</td>\n      <td>8402.88</td>\n    </tr>\n    <tr>\n      <th>12</th>\n      <td>2023-11-13</td>\n      <td>2765.51</td>\n    </tr>\n    <tr>\n      <th>13</th>\n      <td>2023-11-14</td>\n      <td>2608.06</td>\n    </tr>\n    <tr>\n      <th>14</th>\n      <td>2023-11-15</td>\n      <td>2565.58</td>\n    </tr>\n    <tr>\n      <th>15</th>\n      <td>2023-11-16</td>\n      <td>2589.46</td>\n    </tr>\n    <tr>\n      <th>16</th>\n      <td>2023-11-17</td>\n      <td>4645.31</td>\n    </tr>\n    <tr>\n      <th>17</th>\n      <td>2023-11-18</td>\n      <td>9727.49</td>\n    </tr>\n    <tr>\n      <th>18</th>\n      <td>2023-11-19</td>\n      <td>7110.86</td>\n    </tr>\n    <tr>\n      <th>19</th>\n      <td>2023-11-20</td>\n      <td>2341.72</td>\n    </tr>\n    <tr>\n      <th>20</th>\n      <td>2023-11-21</td>\n      <td>2288.61</td>\n    </tr>\n  </tbody>\n</table>\n</div>"
     },
     "execution_count": 9,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df_total_box = df[['movie_box_split_unit','cur_date']].groupby(['cur_date']).sum().reset_index()\n",
    "df_total_box"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2023-11-23T12:31:34.382751300Z",
     "start_time": "2023-11-23T12:31:34.346968800Z"
    }
   },
   "id": "4780ad7702d04008"
  },
  {
   "cell_type": "code",
   "execution_count": 34,
   "outputs": [
    {
     "data": {
      "text/plain": "[{'value': 44179.18, 'name': '拯救嫌疑人'},\n {'value': 12154.24, 'name': '无价之宝'},\n {'value': 10808.06, 'name': '惊奇队长2'},\n {'value': 6553.7, 'name': '河边的错误'},\n {'value': 6264.47, 'name': '二手杰作'},\n {'value': 4002.9, 'name': '饥饿游戏：鸣鸟与蛇之歌'},\n {'value': 3850.42, 'name': '志愿军：雄兵出击'},\n {'value': 2840.56, 'name': '前任4：英年早婚'},\n {'value': 2719.23, 'name': '坚如磐石'},\n {'value': 2044.92, 'name': '追缉'},\n {'value': 12124.27, 'name': '其他'}]"
     },
     "execution_count": 34,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "total_box = round(df[['movie_box_split_unit']].sum().values.tolist()[0],2)\n",
    "other_box = round(total_box -  df_box_top10['movie_box_split_unit'].sum(),2)\n",
    "box_ratio_list = [{'value':round(i[1],2),'name':i[0]}for i in df_box_top10.values.tolist()]\n",
    "box_ratio_list.append({'value':other_box,'name':'其他'})\n",
    "box_ratio_list"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2023-11-23T07:59:29.961638500Z",
     "start_time": "2023-11-23T07:59:29.956190100Z"
    }
   },
   "id": "c0352f7e8bbb798a"
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "outputs": [
    {
     "data": {
      "text/plain": "      movie_box_split_unit  movie_show_count\n0                   904.27             82864\n1                   496.16             54513\n2                   319.74             43835\n3                   116.75             14467\n4                    69.92              6708\n...                    ...               ...\n2858                 45.34              3519\n2859                 24.53               352\n2860                 18.17                68\n2861                 17.56              6729\n2862                 13.95              2981\n\n[334 rows x 2 columns]",
      "text/html": "<div>\n<style scoped>\n    .dataframe tbody tr th:only-of-type {\n        vertical-align: middle;\n    }\n\n    .dataframe tbody tr th {\n        vertical-align: top;\n    }\n\n    .dataframe thead th {\n        text-align: right;\n    }\n</style>\n<table border=\"1\" class=\"dataframe\">\n  <thead>\n    <tr style=\"text-align: right;\">\n      <th></th>\n      <th>movie_box_split_unit</th>\n      <th>movie_show_count</th>\n    </tr>\n  </thead>\n  <tbody>\n    <tr>\n      <th>0</th>\n      <td>904.27</td>\n      <td>82864</td>\n    </tr>\n    <tr>\n      <th>1</th>\n      <td>496.16</td>\n      <td>54513</td>\n    </tr>\n    <tr>\n      <th>2</th>\n      <td>319.74</td>\n      <td>43835</td>\n    </tr>\n    <tr>\n      <th>3</th>\n      <td>116.75</td>\n      <td>14467</td>\n    </tr>\n    <tr>\n      <th>4</th>\n      <td>69.92</td>\n      <td>6708</td>\n    </tr>\n    <tr>\n      <th>...</th>\n      <td>...</td>\n      <td>...</td>\n    </tr>\n    <tr>\n      <th>2858</th>\n      <td>45.34</td>\n      <td>3519</td>\n    </tr>\n    <tr>\n      <th>2859</th>\n      <td>24.53</td>\n      <td>352</td>\n    </tr>\n    <tr>\n      <th>2860</th>\n      <td>18.17</td>\n      <td>68</td>\n    </tr>\n    <tr>\n      <th>2861</th>\n      <td>17.56</td>\n      <td>6729</td>\n    </tr>\n    <tr>\n      <th>2862</th>\n      <td>13.95</td>\n      <td>2981</td>\n    </tr>\n  </tbody>\n</table>\n<p>334 rows × 2 columns</p>\n</div>"
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df_scatter = df[['movie_box_split_unit','movie_show_count']]\n",
    "df_scatter = df_scatter[(df_scatter['movie_box_split_unit'] > 10) & (df_scatter['movie_show_count'] > 10)]\n",
    "df_scatter"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2023-11-23T11:15:36.444743100Z",
     "start_time": "2023-11-23T11:15:36.433254600Z"
    }
   },
   "id": "1a56af2cc058ed1a"
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "outputs": [],
   "source": [
    "from sklearn.linear_model import LinearRegression\n",
    "\n",
    "df_scatter = df_scatter.to_numpy()\n",
    "data = np.array(df_scatter)\n",
    "X = data[:, 0].reshape(-1, 1)\n",
    "y = data[:, 1].reshape(-1, 1)\n",
    "\n",
    "reg = LinearRegression().fit(X, y)\n",
    "slope = reg.coef_[0]\n",
    "intercept = reg.intercept_\n",
    "\n",
    "X_fit = np.linspace(X.min(), X.max(), 100)\n",
    "y_fit = slope * X_fit + intercept\n",
    "df_regression_line = pd.DataFrame({'X_fit': X_fit, 'y_fit': y_fit})\n",
    "df_regression_line.to_csv('../static/data/liner_Regression.csv', index=False)"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2023-11-23T11:16:07.896539Z",
     "start_time": "2023-11-23T11:16:04.979614500Z"
    }
   },
   "id": "8026db40f7a8b167"
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 2
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython2",
   "version": "2.7.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
